package client_gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.IOException;

import javax.media.CaptureDeviceManager;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

import streaming.StreamingConfigurer;
import utils.ImageCreator;
import actions.StreamingSettingsAction;


public class StreamingConfigDialog extends JDialog {


	private StreamingConfigurer config = new StreamingConfigurer();

	public StreamingConfigDialog(Frame owner) {
		super(owner);
//		try {
//			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
//		} catch (ClassNotFoundException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (InstantiationException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (IllegalAccessException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (UnsupportedLookAndFeelException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		initComponents();
	}

	public StreamingConfigDialog(Dialog owner) {
		super(owner);
		initComponents();
	}

	private void okButtonActionPerformed(ActionEvent e) {
		// TODO add your code here

		config.save();
		this.dispose();
	}

	private void cancelButtonActionPerformed(ActionEvent e) {
		// TODO add your code here
		config.revertChanges();
		repaint();
		audioDevicesCombo.setSelectedItem(config.getSelectedAudioDevice());
		videoDevicesCombo.setSelectedItem(config.getSelectedVideoDevice());
		SwingUtilities.updateComponentTreeUI(this);
		this.dispose();
	}

	private void audioDevicesComboItemStateChanged(ItemEvent e) {
		// TODO add your code here
		if(e.getStateChange() == ItemEvent.SELECTED){
			config.setSelectedAudioDevice((String)e.getItem());
		}
	}

	private void videoDevicesComboItemStateChanged(ItemEvent e) {
		// TODO add your code here
		if(e.getStateChange() == ItemEvent.SELECTED){
			config.setSelectedVideoDevice((String)e.getItem());
		}
	}
//	int indent = -1;
//	private void getComponentTree(Container cont){
//		indent ++;
//		String indentString = "";
//		for(int i = 0; i < indent; i++){
//			indentString += "\t";
//		}
//		Component[] components = cont.getComponents();
//		for(int i = 0; i < components.length; i++){
//			System.out.println(String.format(indentString) + components[i].getClass() + " : "  + i);
//			for(Object o : components[i].getListeners(EventListener.class)){
//				System.out.println("  Listener: " + o);
//			}
//			if(components[i] instanceof Container){
//				getComponentTree((Container)components[i]);
//			}
//			else if(components[i] instanceof Button){
//				Button capture = (Button)components[i];
//				if(capture.getActionCommand().equals("Detect Capture Devices")){
//					
//					capture.addActionListener(new ActionListener(){
//
//						@Override
//						public void actionPerformed(ActionEvent e) {
//							// TODO Auto-generated method stub
//							System.out.println(e);
//						}
//						
//					});
//					ActionEvent e = new ActionEvent(capture, ActionEvent.ACTION_PERFORMED, "Detect Capture Devices");
////					capture.action(new Event(capture, ActionEvent.ACTION_PERFORMED, "Detect Capture Devices"), capture);
////					capture.deliverEvent();
//					for(ActionListener a : capture.getActionListeners()){
//						a.actionPerformed(e);
//					}
//				}
//			}
//		}
//		indent--;
//	}

	private void detectButtonActionPerformed(ActionEvent e) {
		// TODO add your code here
//		CDMPanel panel = new CDMPanel();
//		JFrame fr = new JFrame();
//		
//		fr.setBounds(0, 0, 300, 300);
//		fr.getContentPane().add(panel);
//		fr.setVisible(true);
//		for(Component c : panel.getComponents()){
//		System.out.println(c.getClass() + " : "  + c.get);
//		}
//		System.out.println("Listeners");
//		for(Object o : panel.getListeners(EventListener.class)){
//			System.out.println("  Listener: " + o);
//		}
//		getComponentTree(panel);
//		panel.setVisible(false);
		
//		System.out.println("===============================> BEFORE DETECT");
//		for(Object o : CaptureDeviceManager.getDeviceList(null)){
//			System.out.println(((CaptureDeviceInfo)o).getName());
//		}
		
		
		this.setCursor(new Cursor(Cursor.WAIT_CURSOR));
		config.detectDevices();
//		System.out.println("===============================> AFTER DETECT");
//		
//		for(Object o : CaptureDeviceManager.getDeviceList(null)){
//			System.out.println(((CaptureDeviceInfo)o).getName());
//		}
		
		try {
			CaptureDeviceManager.commit();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		config.refreshDevices();
		this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
		repaint();

		
	}


	private void initComponents() {
		titlePanel = new JPanel();
		titleLabel = new JLabel();
		label1 = new JLabel();
		panel3 = new JPanel();
		okButton = new JButton();
		cancelButton = new JButton();
		panel4 = new JPanel();
		devicesPanel = new JPanel();
		audioDeviceLabel = new JLabel();
		audioDevicesCombo = new JComboBox(config.new AudioComboBoxModel());
//		audioDevicesCombo.setSelectedItem(config.getSelectedAudioDevice());
		videoDeviceLabel = new JLabel();
		videoDevicesCombo = new JComboBox(config.new VideoComboBoxModel());
//		videoDevicesCombo.setSelectedItem(config.getSelectedVideoDevice());
		detectButton = new JButton();

		//======== this ========
		setTitle("Streaming Settings");
		setResizable(false);
		Container contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());


		//======== titlePanel ========
		{
			titlePanel.setBackground(Color.white);
			titlePanel.setBorder(new EmptyBorder(10, 10, 10, 10));
			titlePanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
			((FlowLayout)titlePanel.getLayout()).setAlignOnBaseline(true);

			//---- titleLabel ----
			titleLabel.setText("Streaming Configuration");
			titleLabel.setBackground(Color.white);
			titleLabel.setIcon(null);
			titleLabel.setVerticalAlignment(SwingConstants.TOP);
			titleLabel.setFont(titleLabel.getFont().deriveFont(titleLabel.getFont().getStyle() | Font.BOLD, titleLabel.getFont().getSize() + 3f));
			titlePanel.add(titleLabel);

			//---- label1 ----
			label1.setIcon(ImageCreator.createImageIcon(StreamingSettingsAction.class, "images/options.png"));
			titlePanel.add(label1);
		}
		contentPane.add(titlePanel, BorderLayout.NORTH);


		//======== panel3 ========
		{
			panel3.setLayout(new FlowLayout(FlowLayout.TRAILING));

			//---- okButton ----
			okButton.setText("OK");
			okButton.setPreferredSize(new Dimension(80, 20));
			okButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					okButtonActionPerformed(e);
				}
			});
			panel3.add(okButton);

			//---- cancelButton ----
			cancelButton.setText("Cancel");
			cancelButton.setPreferredSize(new Dimension(80, 20));
			cancelButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					cancelButtonActionPerformed(e);
				}
			});
			panel3.add(cancelButton);
		}
		contentPane.add(panel3, BorderLayout.SOUTH);

		//======== panel4 ========
		{
			panel4.setLayout(new BoxLayout(panel4, BoxLayout.X_AXIS));

			//======== devicesPanel ========
			{
				devicesPanel.setLayout(new GridBagLayout());
				((GridBagLayout)devicesPanel.getLayout()).columnWidths = new int[] {50, 150, 50};
				((GridBagLayout)devicesPanel.getLayout()).rowHeights = new int[] {0, 0, 0, 0, 0, 0};
				((GridBagLayout)devicesPanel.getLayout()).columnWeights = new double[] {1.0, 1.0, 1.0};
				((GridBagLayout)devicesPanel.getLayout()).rowWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 1.0E-4};

				//---- audioDeviceLabel ----
				audioDeviceLabel.setText("Audio Device: ");
				audioDeviceLabel.setHorizontalAlignment(SwingConstants.LEFT);
				devicesPanel.add(audioDeviceLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
						GridBagConstraints.CENTER, GridBagConstraints.BOTH,
						new Insets(30, 10, 5, 5), 0, 0));

				//---- audioDevicesCombo ----
				audioDevicesCombo.addItemListener(new ItemListener() {
					public void itemStateChanged(ItemEvent e) {
						audioDevicesComboItemStateChanged(e);
					}
				});

				devicesPanel.add(audioDevicesCombo, new GridBagConstraints(1, 0, 2, 1, 0.0, 0.0,
						GridBagConstraints.CENTER, GridBagConstraints.BOTH,
						new Insets(30, 0, 5, 15), 0, 0));

				//---- videoDeviceLabel ----
				videoDeviceLabel.setText("Video Device: ");
				videoDeviceLabel.setHorizontalAlignment(SwingConstants.LEFT);
				devicesPanel.add(videoDeviceLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
						GridBagConstraints.CENTER, GridBagConstraints.BOTH,
						new Insets(0, 10, 5, 5), 0, 0));

				//---- videoDevicesCombo ----
				videoDevicesCombo.addItemListener(new ItemListener() {
					public void itemStateChanged(ItemEvent e) {
						videoDevicesComboItemStateChanged(e);
					}
				});
				devicesPanel.add(videoDevicesCombo, new GridBagConstraints(1, 1, 2, 1, 0.0, 0.0,
						GridBagConstraints.CENTER, GridBagConstraints.BOTH,
						new Insets(0, 0, 5, 15), 0, 0));

				//---- detectButton ----
				detectButton.setToolTipText("Detect devices");
				detectButton.setBorder(UIManager.getBorder("Button.border"));
				detectButton.setText("Detect");
				detectButton.setPreferredSize(new Dimension(80, 20));
				detectButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						detectButtonActionPerformed(e);
					}
				});
				devicesPanel.add(detectButton, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0,
						GridBagConstraints.CENTER, GridBagConstraints.BOTH,
						new Insets(0, 0, 50, 15), 0, 0));

			}
			panel4.add(devicesPanel);
		}
		contentPane.add(panel4, BorderLayout.CENTER);
//		setSize(360, 300);
		pack();
		setLocationRelativeTo(getOwner());
		// JFormDesigner - End of component initialization  //GEN-END:initComponents
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
	// Generated using JFormDesigner Open Source Project license - unknown
	private JPanel titlePanel;
	private JLabel titleLabel;
	private JLabel label1;
	private JPanel panel3;
	private JButton okButton;
	private JButton cancelButton;
	private JPanel panel4;
	private JPanel devicesPanel;
	private JLabel audioDeviceLabel;
	private JComboBox audioDevicesCombo;
	private JLabel videoDeviceLabel;
	private JComboBox videoDevicesCombo;
	private JButton detectButton;
	// JFormDesigner - End of variables declaration  //GEN-END:variables

	public static void main(String[] args) {
		JFrame frame = new JFrame();

		new StreamingConfigDialog(frame).setVisible(true);
	}
}
